<?php

namespace app\http\middleware;
use app\index\common\JwtAuth;
use app\index\common\ApiResponse;
class Jwt
{
    public function handle($request, \Closure $next)
    {
        $token = $request->param('token');
        $jwtAuth = new JwtAuth();
        if (!$token){
            return ApiResponse::json(401,'token 不能为空',null);
        }

        if ($jwtAuth->verifyToken($token)) {
            // 解析 JWT
            $payload = $jwtAuth->getPayload($token);

            // 将解析后的数据存储到请求中，以便后续控制器可以访问
            $request->jwtPayload = $payload;
        } else {
            // JWT验证失败
            abort(401,'Access denied',null);
        }
        return $next($request);
    }
}
